import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Login',
    component: ()=>import('@/components/login/Login')
  },
  {
    path: '/home',
    name: 'Home',
    component: ()=>import('@/components/home/Home.vue'),
    redirect: '/welcome',
    children:[{
      path: '/welcome',
      name:'Welcome',
      component:()=>import('@/components/welcome/Welcome.vue')
    },
    {
      path: '/user/list',
      name:'SysUserList',
      component:()=>import('@/components/system/SysUserList.vue')
    },
    {
      path: '/role/list',
      name:'SysRoleList',
      component:()=>import('@/components/system/SysRoleList.vue')
    },
    {
      path: '/menu/list',
      name:'SysMenuList',
      component:()=>import('@/components/system/SysMenuList.vue')
    },
  ]
  },


]

const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next)=>{
  //如果用户访问登录页，直接放行
  if(to.path=='/') return next();
  //从sessionStorage中获取到保存的token值
  const token =window.sessionStorage.getItem('token')
  //没有token,强制跳转登录页
  if(!token) return next("/")
    next();
})

export default router
